package action.concreteActions;

import grammer.DirectObject;
import grammer.Predicate;
import grammer.components.ObjectEnum;
import grammer.components.PredicateEnum;
import houseHold.House;
import person.PersonAchive;
import timer.Timer;
import action.TimedActionInterface;

/**
 * 
 * @author Minodor
 * 
 *         Used for predicates:
 */
public class CompoundUseAction extends TimedActionInterface {
	private ObjectEnum oType;

	private int type = 0;

	public CompoundUseAction(Timer startTime, Timer endTime, Predicate p) {
		super(startTime, endTime);
		if (p.getPredicate().equals(PredicateEnum.SHOWERS)) {
			type = 4;
			oType = ObjectEnum.SHOWER;

		}
		if (p.getPredicate().equals(PredicateEnum.TAKES_A_BATH)) {
			type = 4;
			oType = ObjectEnum.BATHTUB;
		}
		if (p.getPredicate().equals(PredicateEnum.EATS)) {
			type = 5;
		}
	}

	public CompoundUseAction(Timer startTime, Timer endTime, Predicate p,
			DirectObject d) {
		super(startTime, endTime);
		oType = d.getObject();
		if (p.getPredicate().equals(PredicateEnum.WASHES)) {
			type = 2;
		} else {
			type = 3;
		}
	}

	@Override
	public int execute() {
		switch (type) {
		case 2: {
			if (PersonAchive.getPerson(this.getOnwer()) != null) {
				if (oType == ObjectEnum.HIMSELF || oType == ObjectEnum.HERSELF) {

					House.getInstance().getRoom(
							PersonAchive.getPerson(this.getOnwer())
									.getCurrentLocation().getCurrent())
							.getItemByType(ObjectEnum.SINK).setActive(true);
					House.getInstance().getRoom(
							PersonAchive.getPerson(this.getOnwer())
									.getCurrentLocation().getCurrent())
							.getItemByType(ObjectEnum.SINK).setActiveDuration(
									getExecuteTimer());

				} else {
					House.getInstance().getRoom(
							PersonAchive.getPerson(this.getOnwer())
									.getCurrentLocation().getCurrent())
							.getItemByType(oType).setActive(true);
					House.getInstance().getRoom(
							PersonAchive.getPerson(this.getOnwer())
									.getCurrentLocation().getCurrent())
							.getItemByType(oType).setActiveDuration(
									getExecuteTimer());
				}
			}
			break;
		}
		case 3: {
			if (PersonAchive.getPerson(this.getOnwer()) != null) {
				House.getInstance().getRoom(
						PersonAchive.getPerson(this.getOnwer())
								.getCurrentLocation().getCurrent())
						.getItemByType(oType).setActive(true);
				House.getInstance().getRoom(
						PersonAchive.getPerson(this.getOnwer())
								.getCurrentLocation().getCurrent())
						.getItemByType(oType).setActiveDuration(
								getExecuteTimer());
			}
			break;
		}
		case 4: {
			if (PersonAchive.getPerson(this.getOnwer()) != null) {
				House.getInstance().getRoom(
						PersonAchive.getPerson(this.getOnwer())
								.getCurrentLocation().getCurrent())
						.getItemByType(oType).setActive(true);
				House.getInstance().getRoom(
						PersonAchive.getPerson(this.getOnwer())
								.getCurrentLocation().getCurrent())
						.getItemByType(oType).setActiveDuration(
								getExecuteTimer());
			}
			break;
		}
		case 5: {
			break;
		}
		}
		return 0;
	}

	@Override
	/**
	 * returns action finish time;
	 */
	public int getEndTimer() {
		return endTime.getTime();
	}

	@Override
	/**
	 * return action duration;
	 */
	public int getExecuteTimer() {

		return getEndTimer() - getStartTimer();
	}

	@Override
	/**
	 * returns action start time;
	 */
	public int getStartTimer() {

		return startTime.getTime();
	}

}
